package com.daniel.thingofday;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class subPage extends Activity{
EditText website;
EditText title;
EditText desc;
EditText date;
Button submit;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.submitpage);
		website = (EditText) findViewById(R.id.inputSite);
		title = (EditText) findViewById(R.id.inputTitle);
		desc = (EditText) findViewById(R.id.inputDesc);
		date = (EditText) findViewById(R.id.inputDate);
		submit = (Button) findViewById(R.id.submit);
		submit.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//Go back to main page
				boolean didWork = doWebUpdate(date.getText().toString(), title.getText().toString(), website.getText().toString(), desc.getText().toString());
				if(didWork)
				{
					Toast.makeText(getApplicationContext(), "Added!", Toast.LENGTH_LONG);
				}
				else
				{
					Toast.makeText(getApplicationContext(), "Error!", Toast.LENGTH_LONG);
				}
			}
		});
	}
	
	private boolean doWebUpdate(String date, String valOne, String valTwo, String valThree)
	{
		 // Create the httpclient
        HttpClient httpclient = new DefaultHttpClient();
       
        // Prepare a request object
        HttpGet httpget = new HttpGet("http://mcauthserv.net76.net/add.php?date=" + date + "&valOne=" + valOne + "&valTwo=" + valTwo + "&valThree=" + valThree);
        httpget.setHeader("Cache-Control", "no-cache");
        // Declare a response
        HttpResponse response;
     
        try {
			response = httpclient.execute(httpget);
	        if(response.getStatusLine().getStatusCode() == 200)
	        {
	        	return true;
	        }
	        else
	        {
	        	return false;
	        }
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return false;
	}
	
}